using System;
using System.Linq;
using mock_ehr_lib.Model.Quality;
using System.Configuration;

namespace measures_service
{
    public class MeasuresRepository
    {
        private static readonly Measure[] _measures;
        static  MeasuresRepository()
        {
            _measures = new[]
                            {
                                new Measure
                                    {
                                        Category = "Structure",
                                        Question = "Patient smoking status.",
                                        NqfNo = "0033",
                                        Description = "",
                                        EvidenceSummary = "",
                                        ExclusionQuestions = new string[]
                                                                 {
                                                                 },
                                        InfoUrl = ConfigurationManager.AppSettings["smokingStatusUrl"],
                                        EligibilityEvaluator =
                                            (patientSummary) => true,
                                        ResultEvaluator = (patientSummary) =>
                                                              {
                                                                  var result = new MeasureEvaluateResult
                                                                                   {
                                                                                       NqfNo = "0033",
                                                                                       Evaluation =
                                                                                           patientSummary.SocialHistory.
                                                                                           Any(
                                                                                               i =>
                                                                                               i.Description.ToLower().
                                                                                                   Contains("smok") ||
                                                                                               i.Description.ToLower().
                                                                                                   Contains("cigar")),
                                                                                   };
                                                                  result.Message = result.Evaluation
                                                                                       ? "Smoking status is documented."
                                                                                       : "Patient is over 13 years of age and smoking status is not documented.";
                                                                  return result;
                                                              }
                                    },
                                new Measure
                                    {
                                        Category = "Core",
                                        Question = "Was the patient advised to cease smoking?",
                                        NqfNo = "0027",
                                        Description = "",
                                        EvidenceSummary = "",
                                        ExclusionQuestions = new string[]
                                                                 {
                                                                 },
                                        InfoUrl =
                                            "http://www.qualityforum.org/QPS/MeasureDetails.aspx?standardID=390&print=1&entityTypeID=1",
                                        EligibilityEvaluator =
                                            (patientSummary) =>
                                            patientSummary.SocialHistory.Any(i => i.Value.Contains("pack")),
                                        ResultEvaluator = (patientSummary) =>
                                                              {
                                                                  var result = new MeasureEvaluateResult
                                                                             {
                                                                                 NqfNo = "0027",
                                                                                 Evaluation = patientSummary.SocialHistory.Any(i => i.Value.ToLower().Contains("counsel")),
                                                                             };
                                                                  result.Message = result.Evaluation
                                                                                       ? "Smoking cessation advice is not documented."
                                                                                       : "Patient is a documented smoker and smoking cessation advice is undocumented.";
                                                                  return result;
                                                              }
                                    },
                                    new Measure
                                    {
                                        Category = "Core",
                                        Question = "Was the patient vaccinated for pneumococcal disease?",
                                        NqfNo = "0043",
                                        Description = @"",
                                        EvidenceSummary = @"",
                                        ExclusionQuestions = new string[]
                                                                 {
                                                                 },
                                        InfoUrl =
                                            "http://www.qualityforum.org/QPS/MeasureDetails.aspx?standardID=492&print=1&entityTypeID=1",
                                        EligibilityEvaluator = (patientSummary) =>
                                                                   {
                                                                       DateTime now = DateTime.Today;
                                                                       if (!patientSummary.DOB.HasValue) return false;
                                                                       var bday = patientSummary.DOB.Value;
                                                                       int age = now.Year - bday.Year;
                                                                       if (bday > now.AddYears(-age)) age--;
                                                                       if (age > 64)
                                                                       {
                                                                           return true;
                                                                       }
                                                                       return false;
                                                                   },
                                        ResultEvaluator = (patientSummary) =>
                                                              {
                                                                  var result = new MeasureEvaluateResult
                                                                                   {NqfNo = "0043"};
                                                                  if (
                                                                      patientSummary.Immunizations.Any(
                                                                          i => i.Vaccine.ToLower().Contains("pneumo")))
                                                                      // TODO use codes
                                                                  {
                                                                      result.Evaluation = true;
                                                                      result.Message =
                                                                          "Penumonia vaccine administration is documented.";
                                                                  }
                                                                  else
                                                                  {
                                                                      result.Evaluation = false;
                                                                      result.Message =
                                                                          "Patient is 65 years of age or older and vaccine administration is not documented.";
                                                                  }
                                                                  return result;
                                                              }
                                    },
                            };
        }

        public static Measure[] GetMeasures()
        {
            return _measures;
        }
    }
}